package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Comment;
import com.tour.mapper.CommentMapper;
import com.tour.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2020-12-30
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    public ResponseWrapper addComment(Comment comment){
        if (comment == null){
            return ResponseWrapper.markError();
        }else {
            int result = commentMapper.insert(comment);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"评论发布成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper delComment(Integer commentId){
        if (commentId == null){
            return ResponseWrapper.markError();
        }else {
            int result = commentMapper.deleteById(commentId);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"评论删除成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findAllComment(Integer scenicId,Integer pageSize,Integer pageNum){
        Page<Map> page = new Page<>(pageNum,pageSize);
        IPage<Map> iPage = commentMapper.findAllComment(page,scenicId);
        List list = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("data",list);
        wrapper.getData().put("total",iPage.getTotal());
        wrapper.getData().put("pageSize",iPage.getSize());
        wrapper.getData().put("pageNum",iPage.getCurrent());
        return wrapper;
    }



}
